package controller.admin;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import model.*;

@Named(value = "a_memberManagedBean")
@SessionScoped
public class MemberManagedBean implements Serializable {
   
    @EJB
    private LikingsFacade likingsFacade;   
    @EJB
    private CountryFacade countryFacade;    
    @EJB
    private StateFacade stateFacade;
    @EJB
    private Member1Facade member1Facade;  
    
    private List<Member1> AllMemberList = new ArrayList<>();
    private Member1 editedMember = new Member1();
    private List<State> selectStateList = new ArrayList<>();
    private Country editedCountry = new Country();
    private List<String> editedLikings = new ArrayList<>();

    public List<Member1> getAllMemberList() {
        return AllMemberList;
    }

    public void setAllMemberList(List<Member1> AllMemberList) {
        this.AllMemberList = AllMemberList;
    }

    public Member1 getEditedMember() {
        return editedMember;
    }

    public void setEditedMember(Member1 editedMember) {
        this.editedMember = editedMember;
    }

    public Country getEditedCountry() {
        return editedCountry;
    }

    public void setEditedCountry(Country editedCountry) {
        this.editedCountry = editedCountry;
    }

    public List<State> getSelectStateList() {
        return selectStateList;
    }

    public void setSelectStateList(List<State> selectStateList) {
        this.selectStateList = selectStateList;
    }

    public List<String> getEditedLikings() {
        return editedLikings;
    }

    public void setEditedLikings(List<String> editedLikings) {
        this.editedLikings = editedLikings;
    }
    
    /////////////////////////////////////////////////////////////////
    public MemberManagedBean() {
    }
    /////////////////////////////////////////////////////////////////
    
    public void edit(){
        try {
            editedMember.setIDMarital(new Marital(editedMember.getIDMarital().getId()));
            editedMember.setIDState(new State(editedMember.getIDState().getId()));
            editedMember.setIDLanguage(new Language(editedMember.getIDLanguage().getId()));
            editedMember.setIDReligion(new Religion(editedMember.getIDReligion().getId()));
            editedMember.setIDCasle(new Caste(editedMember.getIDCasle().getId()));
            editedMember.setIDPermission(new Role(editedMember.getIDPermission().getId()));
            List<Likings> ListLiking = new ArrayList<>();
            for(String s : editedLikings){
                Integer i = Integer.parseInt(s);
                ListLiking.add(likingsFacade.find(i));
            }
            editedMember.setLikingsList(ListLiking);
            member1Facade.edit_Liking(ListLiking, editedMember.getId());
            this.member1Facade.edit(editedMember);            
            
            refreshsetAllMemberList();
            set_message(FacesMessage.SEVERITY_INFO, "Edit Member", "Edit " +editedMember.getUsername()+ " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Edit Member", "Edit " +editedMember.getUsername()+ " error !!!");
        }
    }
    
    public void remove(Member1 m){
        try {
            this.member1Facade.remove(m);
            refreshsetAllMemberList();
            
            set_message(FacesMessage.SEVERITY_INFO, "Remove Member", "Remove " +m.getUsername()+ " successfull.");
        } catch (Exception e) {
            set_message(FacesMessage.SEVERITY_WARN, "Remove Member", "Remove " +m.getUsername()+ " error !!!");
        }
    }
    /////////////////////////////////////////////////////////////////
    public void refreshsetAllMemberList(){
        try {
            AllMemberList = this.member1Facade.findAll();
        } catch (Exception e) {
            AllMemberList = null;
        }
    }
    
    public void handleStateChange() {
        List<State> Ls = new ArrayList<>();
        //editedMember.getIDState().getIDCountry().getId()
        for(State s : stateFacade.getStateByCountryId(editedCountry.getId())){
            Ls.add(s);
        }
        this.selectStateList = Ls;
    }
    
    public void init_dialog(Member1 m){       
        this.editedCountry = countryFacade.find(m.getIDState().getIDCountry().getId());
        handleStateChange();
        editedLikings.clear();
        for(Likings l : m.getLikingsList()){
            editedLikings.add(l.getId().toString());
        }
        System.out.println(editedLikings.size());
    }

    
    private void set_message(FacesMessage.Severity severity, String title, String message){
        try {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, title, message));
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Error message !!!"));
        }
    }
}
